<template>
  <div class="app-container">
    <el-row :gutter="5" style="margin: 10px">
      <el-col :span="5">
        <div>
          <label>姓名：</label><input v-model="tname"/>
        </div>
      </el-col>
      <el-col :span="3">
        <div>
          <button @click="loadData(1)">搜索</button>
        </div>
      </el-col>
    </el-row>

    <div style="float: left;margin-left: 10px">
      <el-upload
        class="upload-demo"
        action="/api/edu/teacher/import.do"
        multiple
        :limit="1"
        :on-success="showFile"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">导入Excel文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传Excel文件，且不超过5MB</div>
      </el-upload>
    </div>

    <div style="float: right;margin-right: 60%">
      <el-button type="primary" @click="download">导出</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :default-sort="{prop: 'id', order: 'descending'}"
    >
      <el-table-column align="center" label="序号" prop="id"></el-table-column>
      <el-table-column align="center" label="教师名" prop="tname"></el-table-column>
      <el-table-column align="center" label="性别" prop="sex"></el-table-column>
      <el-table-column align="center" label="生日" prop="birthday"></el-table-column>
      <el-table-column align="center" label="政治面貌" prop="politics"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="ctime"></el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="update(scope.row)" size="small">修改</el-button>
          <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px;text-align: center;align-items: center">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="pages"
        v-model:current-page="currpage"
        @current-change="loadData"/>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      currpage: '',
      list: [],
      listLoading: false,
      pages: 1,
      tname:''
    }
  },
  mounted() {
    this.loadData(1)
  },
  methods: {
    showFile(){
      this.$alert('亲，Excel导入成功！', '导入成功', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: `action: ${ action }`
          });
        }
      });
    },
    download(){
      location.href="/api/edu/teacher/export.do";
    },
    loadData(page) {
      //请求接口 实现数据的查询
      this.listLoading = true
      request({
        url: "/edu/teacher/pagewhere.do?size=1&page="+page+"&tname="+this.tname
      }).then(res => {
        this.list = res.data.list;
        this.pages = res.data.pages;
        this.listLoading = false;
      })
    },
    del(id) {
      this.$confirm('亲，是否确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //请求接口 实现数据的删除
        request({
          url: "/edu/teacher/del.do?id=" + id
        }).then(res => {
          if (res.code == 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.loadData(1);
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    update(obj) {
      //设置Vuex的数据
      this.$store.commit("user/setTeacher", obj);
      //跳转页面 路由
      this.$router.push("/teacherupdate");
    }
  }
}
</script>

<style scoped>

</style>
